package main

import (
	"flag"
	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promhttp"
	"log"
	"net/http"
)

type MyHandler struct{}

func (h MyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	handler(w, r)
}
func handler(w http.ResponseWriter, r *http.Request) {
	registry := prometheus.NewRegistry()
	collector := Collector{Ctx: r.Context()}
	registry.MustRegister(collector)
	h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
	h.ServeHTTP(w, r)
}

var addr = flag.String("listen.address", ":8080", "The address to listen on for HTTP requests.")

func main() {
	flag.Parse()
	h := MyHandler{}

	http.Handle("/metrics", h)

	err := http.ListenAndServe(*addr, nil)
	if err != nil {
		log.Fatal(http.ListenAndServe(*addr, nil))
	}
	log.Fatal(http.ListenAndServe(*addr, nil))
}
